/*
 * Copyright 2019-2025 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * https://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.iiifi.kite.core.util;

import org.springframework.lang.Nullable;

/**
 * SystemUtil
 *
 * @author kite@iiifi.com 花朝
 */
public class SystemUtils {
    /**
     * 代码部署于 linux 上，工作默认为 mac 和 Windows
     */
    private static final String OS_NAME_LINUX = "LINUX";

    /**
     * 获取 user home
     */
    @Nullable
    public static final String USER_HOME = getSystemProperty("user.home");

    /**
     * 获取用户地址
     */
    @Nullable
    public static final String USER_DIR = getSystemProperty("user.dir");

    /**
     * 获取用户名
     */
    @Nullable
    public static final String USER_NAME = getSystemProperty("user.name");

    /**
     * os 名
     */
    @Nullable
    public static final String OS_NAME = getSystemProperty("os.name");

    /**
     * <p>
     * Gets a System property, defaulting to {@code null} if the property cannot be read.
     * </p>
     * <p>
     * If a {@code SecurityException} is caught, the return value is {@code null} and a message is
     * written to
     * {@code System.err}.
     * </p>
     *
     * @param property the system property name
     * @return the system property value or {@code null} if a security problem occurs
     */
    @Nullable
    private static String getSystemProperty(final String property) {
        try {
            return System.getProperty(property);
        } catch (final SecurityException ex) {
            return null;
        }
    }

    /**
     * 判断是否为本地开发环境
     *
     * @return booleans
     */
    public static boolean isLinux() {
        return StringUtils.isNotBlank(OS_NAME) && OS_NAME_LINUX.equals(OS_NAME.toUpperCase());
    }

    /**
     * 代码部署于 linux 上，工作默认为 mac 和 Windows
     *
     * @return boolean
     */
    public static boolean isLocalDev() {
        return !SystemUtils.isLinux();
    }
}
